package com.sx;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

/**
 * @author shengx
 * @date 2020/7/25 21:49
 */
public class JedisTest {

    public static void main(String[] args) {
        Set<HostAndPort> servers = new HashSet<HostAndPort>();
        HostAndPort h1 = new HostAndPort("192.168.79.120", 7001);
        HostAndPort h2 = new HostAndPort("192.168.79.120", 7002);
        HostAndPort h3 = new HostAndPort("192.168.79.120", 7003);
        HostAndPort h4 = new HostAndPort("192.168.79.120", 7004);
        HostAndPort h5 = new HostAndPort("192.168.79.120", 7005);
        HostAndPort h6 = new HostAndPort("192.168.79.120", 7006);
        servers.add(h1);
        servers.add(h2);
        servers.add(h3);
        servers.add(h4);
        servers.add(h5);
        servers.add(h6);
        // Jedis连接池配置
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        // 最大空闲连接数, 默认8个
        jedisPoolConfig.setMaxIdle(100);
        // 最大连接数, 默认8个
        jedisPoolConfig.setMaxTotal(500);
        //最小空闲连接数, 默认0
        jedisPoolConfig.setMinIdle(0);
        // 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1
        jedisPoolConfig.setMaxWaitMillis(2000); // 设置2秒
        //对拿到的connection进行validateObject校验
        jedisPoolConfig.setTestOnBorrow(true);
        JedisCluster jedisCluster = new JedisCluster(servers, jedisPoolConfig);

        for (int i = 1000; i < 2000; i++) {
            jedisCluster.set("username" + i, "shengx" + i);
        }
    }
}
